//
//  BRShopSelView.m
//  Chargebull
//
//  Created by 甘卫国 on 2024/1/16.
//

#import "BRShopSelView.h"

@interface BRShopSelView ()<QMUITableViewDelegate,QMUITableViewDataSource>

@end

@implementation BRShopSelView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setUI];
    }
    return self;
}

- (void)req_shopList {
    
    [[BRRequestManager shared] GET:[BRAPI merchantList] params:@{} succeed:^(NSDictionary * _Nonnull dataDict, NSInteger respCode) {
        
       
        NSMutableArray *arr = [BRShopSelModel mj_objectArrayWithKeyValuesArray:dataDict[@"data"]];
        
        BRShopSelModel *all = [[BRShopSelModel alloc] init];
        all.branchId = @"";
        all.branchName = Localized(@"shop_totalMerchant");
        all.isSel = YES;
        
        [arr insertObject:all atIndex:0];
        self.datas = arr;
        [self.tableView reloadData];
        
        
    } failed:^(NSInteger code, NSString * _Nonnull errMsg) {
        
    }];
}

- (void)br_hidden {
    
    [UIView animateWithDuration:0.25 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        self.hidden = YES;
    }];
}

- (void)br_show {
    
    self.hidden = NO;
    [UIView animateWithDuration:0.25 animations:^{
        self.alpha = 1;
    } completion:^(BOOL finished) {
        
    }];
}

- (void)br_show:(CGPoint)point {
    
    
    if (MIN(point.x, point.y) != 0) {
        self.contView.mj_x = point.x;
        self.contView.mj_y = point.y;
    }
    [self br_show];
}

- (void)setDatas:(NSArray *)datas {
    _datas = datas;
    
    NSInteger count = datas.count;
    CGFloat maxH = 462.;
    if (count <= 10) {
        maxH = 44 * count;
    }
    if (count == 0) {
        maxH = 200;
    }
    self.contView.mj_h = maxH;
    
    [self.tableView reloadData];
}

- (void)resetName:(NSString *)name shopId:(NSString *)shopId {
    
    for (BRShopSelModel *dto in self.datas) {
        if ([shopId isEqualToString:dto.branchId]) {
            dto.branchName = name;
        }
    }
    [self.tableView reloadData];
}

#pragma mark - delegate --
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.datas.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 44;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    BRShopSelCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BRShopSelCell"];
    if (cell == nil) {
        cell = [[BRShopSelCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"BRShopSelCell"];
    }
    
    cell.dto = self.datas[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
   
    for (int i = 0; i < self.datas.count; i++) {
        BRShopSelModel *dto = self.datas[i];
        if (i == indexPath.row) {
            dto.isSel = YES;
            // 切换记录id
            USERManager.branchId = dto.branchId;
        }else {
            dto.isSel = NO;
        }
    }
    
    [self.tableView reloadData];
    
    [self br_hidden];
    
    BRShopSelModel *dtoSel = self.datas[indexPath.row];
    NSString *selStr = dtoSel.branchName;
    
    if (self.didSelBlock) {
        self.didSelBlock(selStr);
    }
}


#pragma mark - UI --
- (void)setUI {
    
    self.alpha = 0;
    self.hidden = YES;
    
    UIView *shadowV = [[UIView alloc] init];
    shadowV.backgroundColor = kColorShadow(0.1);
    [self addSubview:shadowV];
    [shadowV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(br_hidden)];
    [shadowV addGestureRecognizer:tap];
    
    CGFloat w = kScreenWidth - 68;
    self.contView.frame = CGRectMake(0, 0, w, 200);
    [self addSubview:self.contView];
    
    [self.contView addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
}

- (UIView *)contView {
    
    if (_contView == nil) {
        _contView = [[UIView alloc] init];
        _contView.layer.cornerRadius = 10;
        _contView.layer.masksToBounds = YES;
        _contView.backgroundColor = UIColor.whiteColor;
    }
    
    return _contView;
}

- (QMUITableView *)tableView{
    if (!_tableView) {
        UITableViewStyle styyle =  UITableViewStylePlain;
        _tableView = [[QMUITableView alloc]initWithFrame:CGRectMake(0,kSafeAreaTopHeight, kScreenWidth, kScreenHeight - kSafeAreaTopHeight) style:styyle];
        _tableView.tableFooterView = [UIView new];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
        if (@available(iOS 15.0, *)) {
            _tableView.sectionHeaderTopPadding = 0;
        }

        if (@available(iOS 15.0, *)) {
            [UITableView appearance].sectionHeaderTopPadding = 0;
        }
      
    }
    return _tableView;
}

@end


@implementation BRShopSelCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setUI];
    }
    return self;
}

- (void)setIsSel:(BOOL)isSel {
    _isSel = isSel;
    self.selImg.hidden = !isSel;
    
    self.contentView.backgroundColor = isSel ? kColorBGEC : UIColor.whiteColor;
}

- (void)setDto:(BRShopSelModel *)dto {
    _dto = dto;
    
    self.titleL.text = dto.branchName;
    
    self.isSel = dto.isSel;
}

#pragma mark - UI --
- (void)setUI {
    
    self.titleL = [BRToolsView br_label:@"--" textColor:UIColor.qd_titleTextColor font:[UIFont fontM:14]];
    [self.contentView addSubview:self.titleL];
    
    [self.titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(14);
//        make.top.offset(14);
//        make.bottom.offset(-14);
        make.right.offset(-40);
        make.height.mas_greaterThanOrEqualTo(16);
        make.centerY.offset(0);
    }];
    
    
    self.selImg = [[UIImageView alloc] initWithImage:UIImageMake(@"arrow_tick")];
    [self.contentView addSubview:self.selImg];
    [self.selImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(20);
        make.centerY.offset(0);
        make.right.offset(-14);
    }];
    
    self.selImg.hidden = !self.isSel;
    
    UIView *line = [BRToolsView getLineView:kColorShadow(0.1)];
    [self.contentView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(16);
        make.right.offset(0);
        make.bottom.offset(0);
        make.height.mas_equalTo(0.5);
    }];
}

@end

@implementation BRShopSelModel


+ (NSDictionary *)mj_replacedKeyFromPropertyName {
    return @{@"branchId":@"id"};
}


@end
